package com.test.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.io.*;

@Service
public class RedisService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public void set(String key, Object value) {
        this.set(key, value, 60 * 60 * 24);
    }

    public void set(String key, Object value, final int expiredSeconds) {
        final String keyf = key;
        final Object valuef = value;
        redisTemplate.execute(
                new RedisCallback<Long>() {
                    @Override
                    public Long doInRedis(RedisConnection connection)
                            throws DataAccessException {
                        byte[] keyb = keyf.getBytes();
                        byte[] valueb = toByteArray(valuef);
                        connection.set(keyb, valueb);
                        if (expiredSeconds > 0) {
                            Long ttl = connection.ttl(keyb);
                            if (expiredSeconds > ttl) {
                                connection.expire(keyb, expiredSeconds);
                            }
                        }
                        return 1L;
                    }
                }
        );
    }

    public Object get(String key) {
       return this.get(key,0);
    }

    public Object get(String key, final int expiredSeconds) {
        final String keyf = (String) key;
        Object object = null;
        object = redisTemplate.execute(
                new RedisCallback<Object>() {
                    @Override
                    public Object doInRedis(RedisConnection connection)
                            throws DataAccessException {

                        byte[] key = keyf.getBytes();
                        byte[] value = connection.get(key);
                        if (value == null) {
                            return null;
                        }
                        if (expiredSeconds > 0) {
                            Long ttl = connection.ttl(key);
                            if (expiredSeconds > ttl) {
                                connection.expire(key, expiredSeconds);
                            }
                        }
                        return toObject(value);

                    }
                });
        return object;
    }

    /**
     * 描述 : <Object转byte[]>. <br>
     * <p>
     * <使用方法说明>
     * </p>
     *
     * @param obj
     * @return
     */
    private byte[] toByteArray(Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray();
            oos.close();
            bos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return bytes;
    }

    /**
     * 描述 : <byte[]转Object>. <br>
     * <p>
     * <使用方法说明>
     * </p>
     *
     * @param bytes
     * @return
     */
    private Object toObject(byte[] bytes) {
        Object obj = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bis);
            obj = ois.readObject();
            ois.close();
            bis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return obj;
    }

}
